package com.crm.system.service.impl.cluePoolManager;

import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.DateUtil;
import com.crm.model.entity.cluePoolManager.CluePoolPublic;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.cluePoolManager.CluePoolQO;
import com.crm.model.vo.cluePoolManager.CluePoolVO;
import com.crm.service.cluePoolManager.CluePoolFormDbService;
import com.crm.system.service.cluePoolManager.CluePoolFormService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author yincuimei
 * @Date 2020/12/25 9:56
 */
@Service
public class CluePoolFormServiceImpl implements CluePoolFormService {
    @Autowired
    private CluePoolFormDbService cluePoolFormDbService;

    /**
     * 管理系统(超级管理员)线索管理--线索公海表单
     *    展示：所有管理模块创建的私有池、分公司公有池、全国公有池未删除的线索池;用户模块创建的未删除的个人池
     *    操作:私有池、分公司公有池、全国公有池能修改、删除、停用/启用线索池;个人池不能修改、删除、没有停用/启用线索池操作
     * @param cluePoolQO           ---线索公海表单查询条件
     * @param pageIndex/ pageSize  ---页码、分页大小
     * @param sortName/sortOrder   ---线索公海表单排序字段及规则
     * @Author yincuimei
     * @return
     */
    @Override
    public DhPageInfo<CluePoolVO> selectAllCluePoolList(CluePoolQO cluePoolQO, Integer pageIndex, Integer pageSize, String sortName, String sortOrder) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        //增加删除状态查询条件(0:正常;1:已删除)
        cluePoolQO.setDeleteStatus((short) StatusCodeEnum.NORMAL_CODE.getState());

        Integer dateUnit = 0;

        if (!StringUtils.isBlank(cluePoolQO.getDateType())) {
            if ("thisWeek".equals(cluePoolQO.getDateType())) {
                //本周
                dateUnit = 1;
            }
            if ("thisMonth".equals(cluePoolQO.getDateType())) {
                //本月
                dateUnit = 2;
            }
            if ("thisYear".equals(cluePoolQO.getDateType())) {
                //本年
                dateUnit = 3;
            }
        }

        //排序
        StringBuilder order = new StringBuilder();
        order.append("defaultMarkOrder DESC");
        if(!StringUtils.isBlank(sortName)&&!StringUtils.isBlank(sortOrder)){
            switch(sortName){
                case "createTm":
                {
                    order.append(",createTm ").append(sortOrder);//创建时间
                }
                break;
                case "createPeopleName":
                {
                    order.append(",createPeopleName ").append(sortOrder);//创建人
                }
                break;
                case "poolName":
                {
                    order.append(",poolName ").append(sortOrder);//线索池名称
                }
                break;
                case "poolCategory":
                {
                    order.append(",poolCategory ").append(sortOrder);//线索池类别
                }
                break;
                case "companyName":
                {
                    order.append(",companyName ").append(sortOrder);//所属公司名称
                }
                break;
                case "poolTag":
                {
                    order.append(",poolTag ").append(sortOrder);//线索池标签
                }
                break;
                case "poolType":
                {
                    order.append(",poolType ").append(sortOrder);//线索池类型
                }
                break;
                default:
                    break;
            }
        }
        List<CluePoolVO> cluePoolVOList = cluePoolFormDbService.selectAllCluePoolList(cluePoolQO,dateUnit,order.toString());
        PageInfo<CluePoolVO> pageInfo = new PageInfo<CluePoolVO>(cluePoolVOList);
        return new DhPageInfo<CluePoolVO>(pageInfo, cluePoolVOList);
    }

    /**
     * 管理系统(普通管理员)线索管理--线索公海表单
     *    展示： 全国池未删除的线索池;创建的所选分公司未删除的分公司公有池;本公司创建的未删除的私有池;本公司创建的个人池
     *    操作: 全国池不能修改、删除、停用/启用线索池;
     *         创建时所选的分公司未删除的分公司的公有池能修改、删除、停用/启用;
     *         本公司创建的未删除的私有池:其中自己创建的私有池能修改、删除、停用/启用; 在权限与详情那配置管理组权限的人能修改、删除、停用/启用,否则只能查看;
     *         个人池不能操作
     * @param cluePoolQO           ---线索公海表单查询条件
     * @param pageIndex/ pageSize  ---页码、分页大小
     * @param sortName/sortOrder   ---线索公海表单排序字段及规则
     * @param userAccount          ---登录人信息
     * @Author yincuimei
     * @return
     */
    @Override
    public DhPageInfo<CluePoolVO> selectNormalCluePoolList(CluePoolQO cluePoolQO,UserAccount userAccount,Integer pageIndex,Integer pageSize,String sortName,String sortOrder) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        //增加删除状态查询条件(0:正常;1:已删除)
        cluePoolQO.setDeleteStatus((short) StatusCodeEnum.NORMAL_CODE.getState());

        String companyId = userAccount.getCompanyId();//获取登录人所在的公司

        Integer dateUnit = 0;

        if (!StringUtils.isBlank(cluePoolQO.getDateType())) {
            if ("thisWeek".equals(cluePoolQO.getDateType())) {
                //本周
                dateUnit = 1;
            }
            if ("thisMonth".equals(cluePoolQO.getDateType())) {
                //本月
                dateUnit = 2;
            }
            if ("thisYear".equals(cluePoolQO.getDateType())) {
                //本年
                dateUnit = 3;
            }
        }

        //排序
        StringBuilder order = new StringBuilder();
        order.append("defaultMarkOrder DESC");
        if(!StringUtils.isBlank(sortName)&&!StringUtils.isBlank(sortOrder)){
            switch(sortName){
                case "createTm":
                {
                    order.append(",createTm ").append(sortOrder);//创建时间
                }
                break;
                case "createPeopleName":
                {
                    order.append(",createPeopleName ").append(sortOrder);//创建人
                }
                break;
                case "poolName":
                {
                    order.append(",poolName ").append(sortOrder);//线索池名称
                }
                break;
                case "poolCategory":
                {
                    order.append(",poolCategory ").append(sortOrder);//线索池类别
                }
                break;
                case "companyName":
                {
                    order.append(",companyName ").append(sortOrder);//所属公司名称
                }
                break;
                case "poolTag":
                {
                    order.append(",poolTag ").append(sortOrder);//线索池标签
                }
                break;
                case "poolType":
                {
                    order.append(",poolType ").append(sortOrder);//线索池类型
                }
                break;
                default:
                    break;
            }
        }
        cluePoolQO.setManagerPeopleId(userAccount.getAccountId());
        List<CluePoolVO> cluePoolVOList = cluePoolFormDbService.selectNormalCluePoolList(cluePoolQO,companyId,dateUnit,order.toString());
        PageInfo<CluePoolVO> pageInfo = new PageInfo<CluePoolVO>(cluePoolVOList);
        return new DhPageInfo<CluePoolVO>(pageInfo, cluePoolVOList);
    }

    /**
     * 线索管理(管理系统)--线索公海表单开启/停用线索池
     * @param poolId      ---线索池主键
     * @param poolState   ---线索池开启/停用状态(全国默认池没有停用/启用操作、个人池没有停用/启用操作)
     * @Author yincuimei
     * @return
     */
    @Override
    public Boolean updatePoolStateById(String poolId,Integer poolState) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//获取登录人信息
        CluePoolPublic cluePoolPublic = new CluePoolPublic();
        if(!StringUtils.isBlank(poolId) && poolState != null){
            cluePoolPublic.setCluePoolPublicId(poolId);//线索池ID
            cluePoolPublic.setPoolState(poolState);//线索池状态(1-开启；2-停用)
        }
        cluePoolPublic.setUpdateTm(new Date());//更新时间
        cluePoolPublic.setUpdatePeopleId(userAccount.getAccountId());//更新人
        return cluePoolFormDbService.updatePoolStateById(cluePoolPublic);
    }
}
